<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span
          class="allDelete"
          @click="$emit('update-history', [])"
        >全部删除</span>
        <span @click="isDeleteShow=false">完成</span>
      </div>
      <van-icon
        name="delete-o"
        v-else
        @click="del"
      />
    </van-cell>
    <van-cell
      v-for="(item,index) in searchHistory"
      :key="index"
      :title="item"
      @click="onDelete(item,index)"
    >
      <van-icon
        name="close"
        v-show="isDeleteShow"
      />
    </van-cell>
  </div>
</template>
<script>
export default {
  props: {
    searchHistory: {
      type: Array,
      required: true
    }
  },
  data: () => ({
    isDeleteShow: false
  }),
  methods: {
    del() {
      this.isDeleteShow = true
    },
    onDelete(item, index) {
      if (this.isDeleteShow) {
        this.searchHistory.splice(index, 1)
        return
      }
      this.$emit('search', item)
    }
  }
}
</script>
<style lang="less">
.search-history {
  .van-cell {
    .allDelete {
      margin-right: 30px;
    }
  }
}
</style>
